package jvm;

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

public class ClassLoadOverried extends ClassLoader{

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        StringBuilder stringBuilder = new StringBuilder(name.substring(name.lastIndexOf(".") + 1) + ".class");
        String s = stringBuilder.toString();
        InputStream resourceAsStream = getClass().getResourceAsStream(s);
        if(Objects.isNull(s)){
            throw new ClassNotFoundException("类异常");
        }
        int available = 0;
        try {
            available = resourceAsStream.available();
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] bytes = new byte[available];
        try {
            resourceAsStream.read(bytes,0,bytes.length);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return this.defineClass(name,bytes,0,bytes.length);
    }
}
